package com.my.bookstore.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 解决乱码的过滤器
 * <p>
 * Created by YJH on 2017/7/19 21:51.
 */
public class EncodingFilter implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String encoding=filterConfig.getServletContext().getInitParameter("encoding");

        servletRequest.setCharacterEncoding(encoding);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
